<?php
/**
 * Created by PhpStorm.
 * User: adminpc
 * Date: 2018/6/5
 * Time: 15:44
 */

namespace app\admin\service;


/*
 * CURL网页抓取
 * */
class Curlear
{
    public static function indexCurl($url)
    {
        //初始化
        $ch = curl_init();
        //请求的url，由形参传入
        curl_setopt($ch, CURLOPT_URL, $url);
        //将得到的数据返回
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //不处理头信息
        curl_setopt($ch, CURLOPT_HEADER, 0);
        //连接超过10秒超时
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        //https请求
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        //执行并获取HTML文档内容 //执行curl
        $output = curl_exec($ch);
        //关闭资源
        curl_close($ch);
        //返回内容
        return $output;
    }

    // 获取背景路径
    public static function huoQu($day = 0)
    {
        //获取必应背景图
        //$day = 0; //默认为0 定义是几就往前推几天
        $url = 'https://cn.bing.com/HPImageArchive.aspx?format=js&idx=' . $day . '&n=1&nc=' . time() . '&pid=hp&video=1';
        $content = Curlear::indexCurl($url);
        $arr_json = json_decode($content);
        foreach ($arr_json->images as $k => $v) {
            $url_path = $v->url;
        }
        $img_url = 'https://cn.bing.com/' . $url_path;
        return $img_url;
        //echo "<img src='$img_url' height=\"540\" width=\"960\">";
    }

}